home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / c / minigl.lha / MiniGL / demos / mtex.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-01  |  8.6 KB  |  496 lines

  1.  
  2. #include <mgl/gl.h>
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>    //OF
  7. #include <math.h>
  8.  
  9. #ifdef __VBCC__
  10.     #pragma amiga-align
  11. #endif
  12.  
  13. #include <proto/intuition.h>
  14. #include <proto/dos.h>
  15. #include <proto/graphics.h>
  16.  
  17. #ifdef __VBCC__
  18.     #pragma default-align
  19. #endif
  20.  
  21. //tex numbers
  22.  
  23. GLuint t1;
  24. GLuint t2;
  25. GLuint t3;
  26. GLuint t4;
  27.  
  28. GLenum currentenv = 0;
  29. GLenum currentblend = 0;
  30. GLuint swap_textures = 0;
  31.  
  32. int TexEnv_Array[3] = {
  33. GL_MODULATE,
  34. GL_DECAL,
  35. GL_REPLACE,
  36. };
  37.  
  38. char TexEnv_Str [3][16] = {
  39. {"GL_MODULATE"},
  40. {"GL_DECAL"},
  41. {"GL_REPLACE"},
  42. };
  43.  
  44. int BlendDst_Array[2] = {
  45. GL_SRC_COLOR,
  46. GL_SRC_ALPHA,
  47. };
  48.  
  49. char BlendDst_Str [2][16] = {
  50. {"GL_SRC_COLOR"},
  51. {"GL_SRC_ALPHA"},
  52. };
  53.  
  54. #if 0
  55. GLubyte *LoadPPM(char *name, GLint *w, GLint *h)
  56. {
  57.     int i;
  58.     unsigned long x,y;
  59.     FILE *f;
  60.     GLubyte *where;
  61.     GLubyte *pCur;
  62.     int psize = 3;
  63.  
  64.     f = fopen(name, "r");
  65.  
  66.     if (!f)
  67.     {
  68.     *w = 0; *h=0;
  69.     return NULL;
  70.     }
  71.  
  72.     i = fscanf(f, "P6\n%ld\n%ld\n255\n", &x, &y);
  73.  
  74.     if (i!= 2)
  75.     {
  76.     printf("Error scanning PPM header\n");
  77.     fclose(f);
  78.     *w = 0; *h = 0;
  79.     return NULL;
  80.     }
  81.  
  82.     *w = x;
  83.     *h = y;
  84.  
  85.     where = (GLubyte*)malloc(x*y*psize);
  86.     if (!where)
  87.     {
  88.     printf("Error out of Memory\n");
  89.     fclose(f);
  90.     *w = 0; *h = 0;
  91.     return NULL;
  92.     }
  93.  
  94.  
  95.     i = fread(where, 1, x*y*3, f);
  96.     fclose(f);
  97.  
  98.     if (i != x*y*3)
  99.     {
  100.     printf("Error while reading file\n");
  101.     free(where);
  102.     *w = 0; *h = 0;
  103.     return NULL;
  104.     }
  105.  
  106.     return where;
  107. }
  108. #endif
  109.  
  110.  
  111. void AddAlpha (GLubyte *in, GLubyte *out, GLuint w, GLuint h, GLboolean alpha_average)
  112. {
  113.     int i,j;
  114.     GLubyte r,g,b;
  115.  
  116.    if(alpha_average == GL_FALSE)
  117.    {
  118.     for (i=0; i<h; i++)
  119.     {
  120.         for(j=0; j<w; j++)
  121.         {
  122.             r = *in++;
  123.             g = *in++;
  124.             b = *in++;
  125.  
  126.             *out++ = r;
  127.             *out++ = g;
  128.             *out++ = b;
  129.             *out++ = 0xFF;
  130.         }
  131.     }
  132.    }
  133.    else
  134.    {
  135.     ULONG alpha;
  136.  
  137.     for (i=0; i<h; i++)
  138.     {
  139.         for(j=0; j<w; j++)
  140.         {
  141.             r = *in++;
  142.             g = *in++;
  143.             b = *in++;
  144.  
  145.             alpha = (ULONG)r+(ULONG)g+(ULONG)b;
  146.             if(alpha) alpha /= 3;
  147.  
  148.             *out++ = r;
  149.             *out++ = g;
  150.             *out++ = b;
  151.             *out++ = (GLubyte)alpha;
  152.         }
  153.     }
  154.    }
  155. }
  156.  
  157. GLubyte *LoadPPM_Alpha(char *name, GLint *w, GLint *h, GLboolean genalpha)
  158. {
  159.     int i;
  160.     unsigned long x,y;
  161.     FILE *f;
  162.     GLubyte *where;
  163.     GLubyte *out;
  164.     GLubyte *pCur;
  165.  
  166.     f = fopen(name, "r");
  167.  
  168.     if (!f)
  169.     {
  170.     *w = 0; *h=0;
  171.     return NULL;
  172.     }
  173.  
  174.     i = fscanf(f, "P6\n%lu\n%lu\n255\n", &x, &y);    //OF (%lu)
  175.  
  176.     if (i!= 2)
  177.     {
  178.     printf("Error scanning PPM header\n");
  179.     fclose(f);
  180.     *w = 0; *h = 0;
  181.     return NULL;
  182.     }
  183.  
  184.     *w = x;
  185.     *h = y;
  186.  
  187.     where = (GLubyte*)malloc(x*y*3);
  188.  
  189.     if (!where)
  190.     {
  191.     printf("Error out of Memory\n");
  192.     fclose(f);
  193.     *w = 0; *h = 0;
  194.     return NULL;
  195.     }
  196.  
  197.     out = (GLubyte*)malloc(x*y*4);
  198.  
  199.     if (!out)
  200.     {
  201.     printf("Error out of Memory\n");
  202.     fclose(f);
  203.     *w = 0; *h = 0;
  204.     return NULL;
  205.     }
  206.  
  207.     i = fread(where, 1, x*y*3, f);
  208.     fclose(f);
  209.  
  210.     if (i != x*y*3)
  211.     {
  212.     printf("Error while reading file\n");
  213.     free(where);
  214.     if(out) free(out);
  215.     *w = 0; *h = 0;
  216.     return NULL;
  217.     }
  218.  
  219.     AddAlpha(where, out, x, y, genalpha);
  220.  
  221.     free(where);
  222.  
  223.     return out;
  224. }
  225.  
  226.  
  227. extern GLcontext mini_CurrentContext;
  228.  
  229. void create_window(int w, int h)
  230. {
  231.        ULONG info;
  232.  
  233.     MGLInit();
  234.  
  235.     mglChooseWindowMode(GL_TRUE);
  236.     mglChoosePixelDepth(16);
  237.     mglChooseNumberOfBuffers(2);
  238.  
  239.     mglCreateContext(0,0,w,h);
  240.  
  241. //    mglLockMode(MGL_LOCK_SMART);
  242.       mglLockMode(MGL_LOCK_MANUAL);  //01-05-02
  243.  
  244. }
  245.  
  246. void delete_window()
  247. {
  248.     mglDeleteContext();
  249.     MGLTerm();
  250. }
  251.  
  252. void switch_buffer()
  253. {
  254.     struct Window *win = mglGetWindowHandle();
  255.     static char buffer[256];
  256.  
  257.     mglSwitchDisplay();
  258.  
  259.     Move(win->RPort, win->Width - 200, 30);
  260.     sprintf(buffer, "GL_MGL_ARB_multitexture");
  261.     Text(win->RPort, buffer, strlen(buffer));
  262.  
  263.     Move(win->RPort, win->Width - 200, 50);
  264.     sprintf(buffer, "Current Unit 1 settings:");
  265.     Text(win->RPort, buffer, strlen(buffer));
  266.  
  267.     Move(win->RPort, win->Width - 200, 60);
  268.     //sprintf(buffer, "TexEnv : %s", &TexEnv_Str[currentenv]);
  269.     sprintf(buffer, "TexEnv : %s", TexEnv_Str[currentenv]); //OF
  270.     Text(win->RPort, buffer, strlen(buffer));
  271.  
  272.     Move(win->RPort, win->Width - 200, 70);
  273.     //sprintf(buffer, "Blend  : GL_ONE  %s", &BlendDst_Str[currentblend]);
  274.     sprintf(buffer, "Blend  : GL_ONE  %s", BlendDst_Str[currentblend]); //OF
  275.     Text(win->RPort, buffer, strlen(buffer));
  276.  
  277.     Move(win->RPort, win->Width - 200, 90);
  278.     sprintf(buffer, "    (E/B toggles Env/Blend)");
  279.     Text(win->RPort, buffer, strlen(buffer));
  280.  
  281.     Move(win->RPort, win->Width - 200, 100);
  282.     sprintf(buffer, "    (T swaps texture sets)");
  283.     Text(win->RPort, buffer, strlen(buffer));
  284. }
  285.  
  286. GLuint create_texture(char *texture, GLboolean alpha)
  287. {
  288.     GLuint t = 0;
  289.     GLint w, h;
  290.     GLubyte *pic;
  291.  
  292.     pic = LoadPPM_Alpha(texture, &w, &h, alpha);
  293.     
  294.  
  295.     if (!pic)
  296.     {
  297.     printf("Error loading texture\n");
  298.     return 0;
  299.     }
  300.  
  301.     glGenTextures(1, &t);
  302.  
  303.     if (glGetError() != GL_NO_ERROR)
  304.     {
  305.     printf("Error generating texture\n");
  306.       if(pic) free(pic);
  307.     return 0;
  308.     }
  309.  
  310.     glBindTexture(GL_TEXTURE_2D, t);
  311.  
  312. if(alpha == GL_TRUE)
  313. {
  314.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pic);
  315. }
  316. else
  317. {
  318.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pic);
  319. }
  320.  
  321.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  322.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  323.  
  324.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  325.  
  326.     if (glGetError() != GL_NO_ERROR)
  327.     {
  328.     printf("Error creating texture\n");
  329.       if(pic) free(pic);
  330.  
  331.     return 0;
  332.     }
  333.  
  334.     if(pic) free(pic);
  335.  
  336.     return t;
  337. }
  338.  
  339.  
  340. void idle()
  341. {
  342.     GLenum BDst;
  343.     static float a = 0.0;
  344.     static float b = 0.0;
  345.  
  346.     static float da = 3.1415927/2.0;
  347.  
  348.     a += 0.002;
  349.     if (a > 360.0)
  350.         a -= 360.0;
  351.  
  352.     mglLockDisplay(); //01-05-02
  353.  
  354.     glClear(GL_COLOR_BUFFER_BIT);
  355.  
  356.     glActiveTextureARB(GL_TEXTURE0_ARB);
  357.  
  358. if(!swap_textures)
  359.     glBindTexture(GL_TEXTURE_2D, t1);
  360. else
  361.     glBindTexture(GL_TEXTURE_2D, t3);
  362.  
  363.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  364.     glEnable(GL_TEXTURE_2D);
  365.  
  366.     glActiveTextureARB(GL_TEXTURE1_ARB);
  367.  
  368. if(!swap_textures)
  369.     glBindTexture(GL_TEXTURE_2D, t2);
  370. else
  371.     glBindTexture(GL_TEXTURE_2D, t4);
  372.  
  373.     glEnable(GL_TEXTURE_2D); //enabled during upload
  374. /*
  375. The following is ignored by MiniGL but is needed for normal ARB multitexturing
  376. */
  377.  
  378.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, (GLenum)TexEnv_Array[currentenv]);
  379.  
  380.  
  381.     glBegin(GL_POLYGON);
  382.  
  383.     glColor3f(sin(a)+0.1, fabs(cos(a))+0.1, sin(a)+0.1);
  384.  
  385.     glMultiTexCoord2fARB(GL_TEXTURE0_ARB, sin(a), cos(a));
  386.     glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
  387.     glVertex3f(-5.0, 5.0, 0.0);
  388.  
  389.     glColor3f(fabs(cos(a))+0.1, fabs(cos(a))+0.1, sin(a)+0.1);
  390.  
  391.     glMultiTexCoord2fARB(GL_TEXTURE0_ARB, sin(a+da), cos(a+da));
  392.     glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
  393.     glVertex3f(5.0, 5.0, 0.0);
  394.  
  395.     glColor3f(fabs(cos(a)+0.1), sin(a)+0.1, fabs(cos(a))+0.1);
  396.  
  397.     glMultiTexCoord2fARB(GL_TEXTURE0_ARB, sin(a+2*da), cos(a+2*da));
  398.     glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
  399.     glVertex3f(5.0, -5.0, 0.0);
  400.  
  401.     glColor3f(sin(a)+0.1, sin(a)+0.1, fabs(cos(a))+0.1);
  402.  
  403.     glMultiTexCoord2fARB(GL_TEXTURE0_ARB, sin(a+3*da), cos(a+3*da));
  404.     glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
  405.     glVertex3f(-5.0, -5.0, 0.0);
  406.  
  407.     glEnd();
  408.  
  409. //draw buffer:
  410.  
  411.     mglDrawMultitexBuffer(GL_ONE, (GLenum)BlendDst_Array[currentblend], (GLenum)TexEnv_Array[currentenv]);
  412.  
  413.     glFinish();
  414.  
  415.     switch_buffer();
  416. }
  417.  
  418. void keys(char c)
  419. {
  420.     if (c == 0x1b)
  421.     mglExit();
  422.  
  423.     switch (c)
  424.     {
  425.         case 'B':
  426.         case 'b':
  427.             currentblend = !currentblend;
  428.             break;
  429.         case 'E':
  430.         case 'e':
  431.             if(currentenv == 2)
  432.                 currentenv = 0;
  433.             else
  434.                 currentenv++;
  435.  
  436.             break;
  437.         case 'T':
  438.         case 't':
  439.             swap_textures = !swap_textures;
  440.         default:
  441.             break;
  442.     }
  443.  
  444. }
  445.  
  446.    
  447. int main()
  448. {
  449.     struct Window *win;
  450.     float a = 0;
  451.     float da = 3.1415927/2.0;
  452.  
  453.     create_window(640, 480);
  454.  
  455.     win = (struct Window *)mglGetWindowHandle();
  456.  
  457.     SetAPen(win->RPort, 2);
  458.  
  459. //upload with unit1 
  460.  
  461.     glActiveTextureARB(GL_TEXTURE1_ARB);
  462.     glEnable(GL_TEXTURE_2D);
  463.  
  464.     t1 = create_texture("data/test1.ppm", GL_FALSE);
  465.     t2 = create_texture("data/test2.ppm", GL_TRUE);
  466.     t3 = create_texture("data/t1.ppm", GL_FALSE);
  467.     t4 = create_texture("data/t2.ppm", GL_TRUE);
  468.  
  469.     printf("Extensions: %s\n", glGetString(GL_EXTENSIONS));
  470.  
  471.     glViewport(0, 0, 640, 480);
  472.     glMatrixMode(GL_PROJECTION);
  473.     glLoadIdentity();
  474.     gluPerspective(75.0, 1.3, 0.1, 10000.0);
  475.     glMatrixMode(GL_MODELVIEW);
  476.     glLoadIdentity();
  477.     gluLookAt(0.0, 0.0, -20.0,  0.0, 0.0, 0.0,  0.0, 1.0, 0.0);
  478.  
  479.     glScalef(3.0, 3.0, 1.0);
  480.     glRotatef(45, 35, 15, 25);
  481.  
  482.     glDisable(GL_CULL_FACE);
  483.     glDisable(GL_DEPTH_TEST);
  484.  
  485.     glColor3f(1.0, 1.0, 1.0);
  486.     glClearColor(0.0, 0.0, 0.0, 0.0);
  487.  
  488.     mglIdleFunc(idle);
  489.     mglKeyFunc(keys);
  490.     mglMainLoop();
  491.  
  492.       delete_window();
  493.  
  494.     return 0;
  495. }
  496.